# -*- coding: utf-8 -*-
# !/usr/bin/env python3

"""
@Author   : Leonis.Li
@Email    : leonis.li.ext@zeiss.com
@Cellphone:
@Version  : 1.0
@License  : Apache Licence
@Script   : base.py
@CreateAt : 2023/10/17 11:13
@UpdateAt : 2023/10/17 11:13
"""


import logging
from logging.handlers import RotatingFileHandler


class DevEnvConfig(object):

    # Debug mode
    DEBUG = False

    # Secret Key
    SECRET_KEY = 'Camel@1539*_B+^%A7~'

    # CSRF Protected close.
    WTF_CSRF_ENABLED = False

    # Logs configures
    LOG_DEBUG_MODE_LEVEL = logging.INFO
    LOG_PRD_MODE_LEVEL = logging.INFO
    LOG_FILE = "./logs/apis.log"
    LOG_ENCODING = "utf-8"
    LOG_FORMATTER = "[%(asctime)s] %(levelname)s : %(message)s [in %(pathname)s:%(lineno)d]"
    LOG_FILE_MAX_BYTES = 1024 * 1024 * 100
    LOG_BACKUP_COUNT = 5

    @classmethod
    def set_logger(cls, flask_app=None):
        """Set flask application logger"""

        if flask_app.debug:
            # Stream handle Output: console(If in Flask debug mode)
            log_handler = logging.StreamHandler()
            # Log level
            log_handler.setLevel(cls.LOG_DEBUG_MODE_LEVEL)
        else:
            # Create LogFile handler(If not in Flask debug mode)
            log_handler = RotatingFileHandler(filename=cls.LOG_FILE,
                                              encoding=cls.LOG_ENCODING,
                                              maxBytes=cls.LOG_FILE_MAX_BYTES,
                                              backupCount=cls.LOG_BACKUP_COUNT)
            # Set Log-Level
            log_handler.setLevel(cls.LOG_PRD_MODE_LEVEL)

        # Set Log-Formatter
        formatter = logging.Formatter(cls.LOG_FORMATTER)
        log_handler.setFormatter(formatter)

        # Add logger handler for Flask application
        flask_app.logger.addHandler(log_handler)

